//
//  PicViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/15.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "PicViewController.h"
#import "PicTableViewCell.h"
#import "PicModel.h"
@interface PicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *picArray;
@property (nonatomic,strong) NSString *urlString;
@end

@implementation PicViewController
- (NSMutableArray *)picArray
{
    if (!_picArray) {
        _picArray = [[NSMutableArray alloc]init];
    }
    return _picArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self nextUrl:kFunPic];
    [self.tableView reloadData];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    self.tableView.allowsSelection = NO;
    self.tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    
    GroundGlass *backImage = [[GroundGlass alloc]initWithFrame:[UIScreen mainScreen].bounds style:UIBlurEffectStyleLight];
    self.tableView.backgroundView = backImage;

    //上拉加载
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
    self.tableView.footer = footer;
    //下来刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
    self.tableView.header = header;
    
}
//下拉刷新
- (void)headerAction
{
    [self nextUrl:kFunPic];
    [self.tableView.header endRefreshing];
}
//上拉加载
- (void)footerAction
{
    NSLog(@"next_url == %@",_urlString);
    [self nextUrl:_urlString];
    
    [self.tableView.footer endRefreshing];
}

- (void)nextUrl:(NSString *)url
{
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = [response objectForKey:@"data"];
        NSArray *array = [dic objectForKey:@"articles"];
        NSDictionary *dicss = [dic objectForKey:@"info"];
        _urlString = [dicss objectForKey:@"next_url"];
        NSLog(@"next = %@",_urlString);
        NSMutableArray *picArr = [NSMutableArray array];
        for (NSMutableDictionary *dict in array) {
            NSArray *arr = [dict objectForKey:@"media"];
            for (NSMutableDictionary *dics in arr) {
                PicModel *model = [[PicModel alloc]init];
                model.title = [dict objectForKey:@"title"];
                model.thumbnail_pic = [dics objectForKey:@"raw_url"];
                [picArr addObject:model];
            }
        }
        [self.picArray addObjectsFromArray:picArr];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.picArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    static NSString *cellIdnetifier = @"picClassCell";
    [tableView registerClass:[PicTableViewCell class] forCellReuseIdentifier:cellIdnetifier];
    
    PicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdnetifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    PicModel *model = _picArray[indexPath.row];
    [cell cellSetModel:model];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PicModel *model = [self.picArray objectAtIndex:indexPath.row];
   PicTableViewCell *cell = [[PicTableViewCell alloc]init];
    ;
   return  [cell height:model];
    }


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
